"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = percent100;
var _big = _interopRequireDefault(require("big.js"));
function percent100(parts, precision = 0) {
  const total = parts.reduce((acc, part) => acc.plus(part), (0, _big.default)(0));
  const exacts = parts.map(part => (0, _big.default)(part).div(total).times(100));
  const results = exacts.map(part => part.round(precision));
  const sum = results.reduce((acc, part) => acc.plus(part), (0, _big.default)(0));
  if (sum.eq(100)) {
    return results.map(item => item.toNumber());
  }
  const diff = sum.minus(100);
  const diffs = results.map((result, i) => ({
    index: i,
    diff: (0, _big.default)(result).minus(exacts[i])
  })).sort((a, b) => b.diff.minus(a.diff).toNumber());
  const number = diff.times((0, _big.default)(10).pow(precision));
  const modifys = number.gt(0) ? diffs.slice(0, number.toNumber()) : diffs.slice(number.toNumber());
  for (let i = 0; i < results.length; i++) {
    if (modifys.some(item => item.index === i)) {
      if (number.gt(0)) {
        results[i] = results[i].minus((0, _big.default)(0.1).pow(precision));
      } else {
        results[i] = results[i].plus((0, _big.default)(0.1).pow(precision));
      }
    }
  }
  return results.map(item => item.toNumber());
}